<?
	include_once("cms.201003310000/config/cmsConfig.php");
	
	
	//just because we can.. and we need to see if we have auth or not.
	session_start();
	
	$id = extractStringWithKeyFromArray('id',$_GET,null);
	$authInfo = extractObjectWithKeyFromArray('authInfo',$_SESSION, null);
	
	// we need to check for auth info and if we are logged in or not.
	// if we are logged in to the cms we have the option of previewing messages also..
	// that will preview based on a mailout object that is not stored in the database.
	
	if ( is_null($id) || strlen($id) != 8) {
		// we need to redirect.. or throw a hissey fit here or something like that..
		header("http/1.0 500 Internal Server Error", true);
		die("No newsletter id specified");
	}
	
	$previewMessage = false;
	if (is_null($authInfo)){
		$authInfo = setupPublicUser();
	} else {
		//we only want to allow previews if we are actually logged in with a session etc..
		$previewMessage = extractBooleanWithKeyFromArray('previewMessage', $_GET, false);		
	}
	
	$mailout = null;
	if ($previewMessage){
		//try and extract it from the session.
		$mailout = extractObjectWithKeyOfClassFromArray('mailout',$CMS_MAILOUT_CLASS, $_SESSION, null);

	} else {
		// load the message... if we can
		$mailout = new $CMS_MAILOUT_CLASS();
		$res = $mailout->load($id, $authInfo , $db);
		//simple tidy up
		if (!$res) $mailout = null;
	}

	
	if (is_null($mailout)){
		header("http/1.0 500 Internal Server Error", true);
		die("Error loading newsletter");
	} else {
		if ($previewMessage) $mailout->takeValueForKey(true, 'previewMode');
		echo $mailout->generateHTML($db);
		if ($previewMessage) $mailout->takeValueForKey(false, 'previewMode');

	}
	
?>